﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DAL;
using Common;

public partial class Stock_WxStock_Out : System.Web.UI.Page
{
    protected int WxStockKey;

    protected string MenuType
    {
        get { return string.IsNullOrEmpty(Request["menutype"]) ? "" : Request["menutype"].ToString(); }
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        WxStockKey = string.IsNullOrEmpty(Request["WxStockKey"]) ? 0 : Convert.ToInt32(Request["WxStockKey"]);

        if (!this.IsPostBack)
        {
            txt_ReceiveTime.Attributes.Add("onclick", "selectDate(this);");

            tr_FinalPrice.Visible = false;
            tr_ChangeStock.Visible = false;
            tr_SalePrice.Visible = false;

            if (WxStockKey != 0)
            {
                Dat_WxStock wxstock = new WxStock().SelectWxStock(WxStockKey);

                txt_InDanNum.Text = wxstock.InDanNum;
                txt_StockName.Text = wxstock.StockName;
                txt_Spec.Text = wxstock.Spec;
                txt_Mistake.Text = wxstock.Mistake;
                txt_ReceiveTime.Text = WebUtility.DateTimeToString(wxstock.ReceiveTime).Split(' ')[0];
                txt_Price.Text = WebUtility.DecimalToString(wxstock.Price);
                txt_MUserKey.Text = new Account().GetAccountName(wxstock.MUserKey);

                btnSubmit.Text = "出 库";
            }
       }
    }

    //出库
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime? dt = null;
        Decimal? de = null;

        if (WxStockKey != 0)
        {
            Dat_WxStock wxstock = null;

            if (txt_WxStatus.SelectedValue == "已修复")
            {
                wxstock = new Dat_WxStock
                {
                    WxStockKey = WxStockKey,
                    WxStatus = txt_WxStatus.SelectedValue,
                    FinalPrice = txt_FinalPrice.Text.Trim() == "" ? de : Convert.ToDecimal(txt_FinalPrice.Text.Trim()),
                    StockStatus = "已出库",
                    OutUserKey = Convert.ToInt32(Session["UserKey"])
                };
            }
            if (txt_WxStatus.SelectedValue == "更换")
            {
                wxstock = new Dat_WxStock
                {
                    WxStockKey = WxStockKey,
                    WxStatus = txt_WxStatus.SelectedValue,
                    ChangeStock = txt_ChangeStock.Text.Trim(),
                    SalePrice = txt_SalePrice.Text.Trim() == "" ? de : Convert.ToDecimal(txt_SalePrice.Text.Trim()),
                    StockStatus = "已出库",
                    OutUserKey = Convert.ToInt32(Session["UserKey"])
                };
            }
            if (txt_WxStatus.SelectedValue == "未修复")
            {
                wxstock = new Dat_WxStock
                {
                    WxStockKey = WxStockKey,
                    WxStatus = txt_WxStatus.SelectedValue,
                    StockStatus = "已出库",
                    OutUserKey = Convert.ToInt32(Session["UserKey"])
                };
            }

            if (new WxStock().OutWxStock(wxstock))
            {
                WebUtility.JsAlertToGo("出库成功", "WxStock.aspx?menutype=362&stockstatus=已入库");
            }
        }
    }

    //wxstatus change
    protected void txt_WxStatus_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (txt_WxStatus.SelectedValue == "已修复")
        {
            tr_FinalPrice.Visible = true;
            tr_ChangeStock.Visible = false;
            tr_SalePrice.Visible = false;
        }
        if (txt_WxStatus.SelectedValue == "更换")
        {
            tr_FinalPrice.Visible = false;
            tr_ChangeStock.Visible = true;
            tr_SalePrice.Visible = true;
        }
        if (txt_WxStatus.SelectedValue == "未修复" || txt_WxStatus.SelectedValue == "")
        {
            tr_FinalPrice.Visible = false;
            tr_ChangeStock.Visible = false;
            tr_SalePrice.Visible = false;
        }
    }
}
